<script setup>
import { ref, watch, onMounted, onUnmounted } from 'vue'
import VChart from 'vue-echarts'

const props = defineProps({
  option: {
    type: Object,
    required: true
  },
  autoresize: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['click'])

const chartRef = ref(null)

watch(() => props.option, (newOption) => {
  if (chartRef.value && newOption) {
    chartRef.value.setOption(newOption)
  }
}, { deep: true })

const handleClick = (params) => {
  emit('click', params)
}

onMounted(() => {
  console.log('Chart mounted')
})

onUnmounted(() => {
  console.log('Chart unmounted')
})
</script>

<template>
  <v-chart
    ref="chartRef"
    :option="option"
    :autoresize="autoresize"
    @click="handleClick"
    class="chart"
  />
</template>

<style scoped>
.chart {
  width: 100%;
  height: 100%;
}
</style>

